<template>
	<view>
		<view v-if="isMsg == 1" class="change_pwd_box">
			<view class="input_item">
				<text>原支付密码</text>
				<u--input type="password" placeholder="请输入原支付密码" border="bottom" maxlength="6" v-model="old_pwd"></u--input>
				<text>支付密码</text>
				<u--input type="password" placeholder="请输入支付密码" border="bottom" maxlength="6" v-model="new_pwd"></u--input>
				<text>确认密码</text>
				<u--input type="password" placeholder="请输入支付密码" border="bottom" maxlength="6" v-model="new_pwds"></u--input>
			</view>
		</view>
		<view v-else class="change_pwd_box">
			<view class="verify">
				<view class="meno">
					请输入{{userinfo.mobile}}收到的短信验证码
				</view>
				<view class="v-code">
					<input class="oldcode" type="number" maxlength="6" :disabled="is_show" v-model="oldcode" placeholder="请输入验证码">
					<view class="v-code-btn" @click="sendCode">
						{{oldcodetext}}
					</view>
				</view>
			</view>
			<view v-if="is_show" class="input_item">
				<text>支付密码</text>
				<u--input type="password" placeholder="请输入支付密码" border="bottom" maxlength="6" v-model="new_pwd"></u--input>
				<text>确认密码</text>
				<u--input type="password" placeholder="请输入支付密码" border="bottom" maxlength="6" v-model="new_pwds"></u--input>
			</view>
		</view>
		<view class="order" @click="_order">{{orderMsg}}</view>
		<view class="messages" @click="setMessage">
			{{messages}}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				old_pwd: '',
				new_pwd: '',
				new_pwds: '',
				typs: 1,
				userinfo: {},
				messages: '忘记原密码，使用验证码修改',
				isMsg: 1,
				oldcode: '',
				isCodeTrueA: true,
				oldcodetext: '获取验证码',
				is_safety_verify: false,
				is_show: false,
				orderMsg: '下一步'
			}
		},
		onLoad() {
			this.userinfo = uni.getStorageSync('userinfo');
			this.getUserInfo5()
		},
		methods: {
			async _order() {
				if (this.isMsg == 1) {
					if (this.old_pwd == '' || this.old_pwd == null) {
						uni.showToast({
							title: '请输入原支付密码',
							icon: 'none'
						})
						return false
					} else {
						var x = /^[0-9]{6,6}$/
						if (!x.test(this.old_pwd)) {
							uni.showToast({
								title: '请输入正确的原支付密码',
								icon: 'none'
							})
							return false
						}
					}
					if (this.new_pwd == '' || this.new_pwd == null) {
						uni.showToast({
							title: '请输入支付密码',
							icon: 'none'
						})
						return false
					} else {
						var x = /^[0-9]{6,6}$/
						if (!x.test(this.new_pwd)) {
							uni.showToast({
								title: '请输入正确的支付密码',
								icon: 'none'
							})
							return false
						}
					}
					if (this.new_pwds == '' || this.new_pwds == null) {
						uni.showToast({
							title: '请输入确认密码',
							icon: 'none'
						})
						return false
					}
					if (this.new_pwd != this.new_pwds) {
						uni.showToast({
							title: '两次密码不正确',
							icon: 'none'
						})
						return false
					}
					this.saveData()
				}
				if (this.isMsg == 2) {
					if (!this.is_safety_verify) {
						this.verify()
					} else {
						if (this.new_pwd == '' || this.new_pwd == null) {
							uni.showToast({
								title: '请输入支付密码',
								icon: 'none'
							})
							return false
						} else {
							var x = /^[0-9]{6,6}$/
							if (!x.test(this.new_pwd)) {
								uni.showToast({
									title: '请输入正确的支付密码',
									icon: 'none'
								})
								return false
							}
						}
						if (this.new_pwds == '' || this.new_pwds == null) {
							uni.showToast({
								title: '请输入确认密码',
								icon: 'none'
							})
							return false
						}
						if (this.new_pwd != this.new_pwds) {
							uni.showToast({
								title: '两次密码不正确',
								icon: 'none'
							})
							return false
						}
						this.saveData()
					}
				}
			},
			async saveData() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Secret/change_pay_pass',
					data: {
						typs: this.typs,
						oldPass: this.old_pwd,
						newPass: this.new_pwd
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				if (res.code == 1) {
					setTimeout(function() {
						uni.navigateBack({
							delta: 1 // 返回的页面数，如果为1表示返回上一页
						});
					},1500)
				}
			},
			// 用户信息
			async getUserInfo5() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'user/getUserInfo',
					header: {
						'token': this.userinfo.token
					}
				});
				if (res.code == 1) {
					res.data.token = this.userinfo.token
					this.userinfo = res.data
					uni.setStorageSync('userinfo', res.data);
				}
			},
			setMessage() {
				this.oldcode = ''
				this.new_pwd = ''
				this.new_pwds = ''
				if (this.isMsg == 1) {
					this.isMsg = 2
					this.typs = 2
					this.messages = "使用原密码修改"
				} else {
					this.typs = 1
					this.isMsg = 1
					this.messages = "忘记原密码，使用验证码修改"
				}
			},
			// 验证 - 手机号验证码
			async verify() {
				if (this.oldcode == '') {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
					return false
				}
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'User/verifyOwn',
					data: {
						mobile: this.userinfo.mobile,
						code: this.oldcode
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.hideLoading()
				if (res.code == 1) {
					this.orderMsg = '确定'
					this.is_show = true
					this.is_safety_verify = true
					this.isCodeTrueA = false
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			// 安全验证 - 发送验证码
			async sendCode() {
				if (this.isCodeTrueA) {
					this.isCodeTrueA = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp5({
						url: 'V1/sendSms',
						data: {
							mobile: this.userinfo.mobile
						},
						header: {
							'token': this.userinfo.token
						}
					});
					uni.hideLoading()
					uni.showToast({
						title: res.msg,
						icon: "none"
					});
					if (res.code == 1) {
						let holdTime = 60; //定义变量并赋值
						this.oldcodetext = "重新获取(60)"
						this.Timer = setInterval(() => {
							if (holdTime <= 0) {
								this.isCodeTrueA = true;
								this.oldcodetext = "获取验证码"
								clearInterval(this.Timer); //清除该函数
								return; //返回前面
							}
							this.oldcodetext = "重新获取(" + holdTime + ")"
							holdTime--;
						}, 1000)
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.change_pwd_box {
		padding: 30rpx;
		box-sizing: border-box;
		.input_item {
			text {
				font-size: 28rpx;
				color: #333;
				padding-left: 10rpx;
				margin: 20rpx 0;
				display: block;
			}
			
			input {
				padding: 10rpx;
				border-bottom: #eeeeee 1px solid;
				font-size: 28rpx;
			}
		}
		
		.verify {
			font-size: 28rpx;
			.meno {
				padding-bottom: 20rpx;
				color: #c8c9cc;
			}
			.v-code {
				display: flex;
				justify-content: space-between;
				border-bottom: 1px solid #f4f4f5;
				padding-bottom: 10rpx;
				
				.oldcode {
					width: 60%;
					height: 70rpx;
				}
				
				.v-code-btn {
					padding: 16rpx 20rpx;
					color: #ffffff;
					background-color: #fa3534;
					border-radius: 20rpx;
				}
			}
		}
	}

	.order {
		background-color: #1296db;
		margin: 50rpx 20rpx;
		text-align: center;
		padding: 30rpx 0;
		border-radius: 10rpx;
		color: #fff;
		transition: all 0.5s;
	}

	.order:active {
		background-color: #1296db;
	}
	
	.messages {
		text-align: center;
		font-size: 24rpx;
	}
</style>
<style>
	.placeholder-class {
		color: #e1e1e1;
	}
</style>
<style scoped lang="scss">
	.placeholder-class {
		color: #e1e1e1;
	}
</style>